package ch02.shiLi;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;

public class Test01 {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, TransformerException {
        //得到DOM解析器的工厂实例
        DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
        //从DOM工厂获取DOM解析器
        DocumentBuilder db=dbf.newDocumentBuilder();
        //解析XML文档，得到一个Document对象
        Document doc=db.parse("src/ch02/shiLi/收藏信息.xml");
        //得到所有Brand节点列表信息
        NodeList brandList = doc.getElementsByTagName("Brand");
        //循环Brand信息
        for (int i = 0; i < brandList.getLength(); i++) {
            Node brand = brandList.item(i);
            Element element = (Element) brand;
            String attrValue = element.getAttribute("name");
            NodeList types = element.getChildNodes();
            for (int j = 0; j < types.getLength(); j++) {
                if(types.item(j).getNodeType() == Node.ELEMENT_NODE){
                    Element typeElement = ((Element) types.item(j));
                    String type = typeElement.getAttribute("name");
                    System.out.println("手机："+attrValue+type);
                }

            }
        }


        //示例3
        //读取pubDate
        NodeList list = doc.getElementsByTagName("pubDate");
        //pubDate元素节点
        Element pubDateElement = (Element) list.item(0);
        //读取文本节点
        String pubDate = pubDateElement.getFirstChild().getNodeValue();
        System.out.println(pubDate);

        //shili4
        //创建Brand节点
        Element brandElement = doc.createElement("Brand");
        brandElement.setAttribute("name","三星");
        //创建Type节点
        Element typeElement = doc.createElement("Type");
        typeElement.setAttribute("name","NoteX");
        //添加父子关系
        brandElement.appendChild(typeElement);
        Element phoneElement = (Element) doc.getElementsByTagName("PhoneInfo").item(0);
        phoneElement.appendChild(brandElement);
        //保存XML文件
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource domSource = new DOMSource(doc);
        //设置编码类型
        transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
        StreamResult result = new StreamResult("src/ch02/shiLi/收藏信息.xml");
        //把DOM转为XML文件
        transformer.transform(domSource,result);

        //示例5修改
        //找到修改的节点
        NodeList list1 = doc.getElementsByTagName("Brand");
        for (int i = 0; i < list1.getLength(); i++) {
            Element brandElement1 = (Element) list1.item(i);
            String brandName = brandElement1.getAttribute("name");
            if (brandName.equals("三星")){
                brandElement1.setAttribute("name","SAMSUNG");
            }
        }
        DOMSource domSource2 = new DOMSource(doc);
        //设置编码类型
        transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
        StreamResult result2 = new StreamResult("src/ch02/shiLi/收藏信息.xml");
        //把DOM转为XML文件
        transformer.transform(domSource2,result2);

        //shili6
        NodeList brand = doc.getElementsByTagName("Brand");
        for (int i = 0; i < brand.getLength(); i++) {
            Element item = (Element) brand.item(i);
            String name = item.getAttribute("name");
            if (name.equals("SAMSUNG")) {
                item.getParentNode().removeChild(item);
            }
        }
        DOMSource domSource3 = new DOMSource(doc);
        //设置编码类型
        transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
        StreamResult result3 = new StreamResult("src/ch02/shiLi/收藏信息.xml");
        //把DOM转为XML文件
        transformer.transform(domSource3,result3);
    }
}
